﻿// connerdetect.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
	char fn[] = "D:\\opencv\\sources\\samples\\data\\box.png";
	Mat img = imread(fn);
	
	Mat reshapeImg;
	resize(img, reshapeImg, Size(500, 500));
	Mat gray;
	cvtColor(reshapeImg, gray, COLOR_BGR2GRAY);
	imshow("reshapeImg", reshapeImg);
	Mat harris;
	int neighbourhood = 2;
	int aperture = 3;
	double k = 0.04;
	cornerHarris(gray, harris, neighbourhood, aperture, k);
	double minStrength, maxStrength;
	minMaxLoc(harris, &minStrength, &maxStrength);
	Mat dilated;
	dilate(harris, dilated, Mat());
	Mat localMax;
	compare(harris, dilated, localMax, CMP_EQ);
	Mat cornerMap,conerTh;
	double qualityLevel = 0.01;
	double thres = qualityLevel * maxStrength;
	threshold(harris, conerTh, thres, 255, THRESH_BINARY);
	Mat result;
	conerTh.convertTo(cornerMap, CV_8U);
	conerTh.convertTo(result, CV_8U);
	bitwise_and(cornerMap, localMax, cornerMap);
	bitwise_and(result, cornerMap, result);
	imshow("result", result);
	vector<Point> points;
	for (int y = 0; y < cornerMap.rows; y++) {
		const uchar*rowPtr = cornerMap.ptr<uchar>(y);
		for (int x = 0; x < cornerMap.cols; x++) {
			if (rowPtr[x]) {
				points.push_back(Point(x,y));
			}
		}
	}
	vector<Point>::const_iterator it = points.begin();
	Scalar color = Scalar(0, 0, 255);
	while (it!=points.end())
	{
		circle(reshapeImg, *it, 3, color);
		it++;
	}
	imshow("harris", reshapeImg);
	waitKey(0);
    //std::cout << "Hello World!\n"; 
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
